package com.example.demo.mapper;

import com.example.demo.dto.CourseInfo;
import com.example.demo.dto.CourseVo;
import com.example.demo.entity.Course;
import com.example.demo.entity.Teacher;
import com.example.demo.entity.CourseCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CourseMapper {
    // 新增课程
    @Insert("INSERT INTO courses (title, description, teacher_id, category_id, status) VALUES (#{title}, #{description}, #{teacher_id}, #{category_id}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertCourse(CourseVo course);

    // 删除课程
    @Delete("DELETE FROM courses WHERE id = #{id}")
    int deleteCourseById(int id);

    // 修改课程
    @Update("UPDATE courses SET title = #{title}, description = #{description}, teacher_id = #{teacher_id}, category_id = #{category_id}, status = #{status} WHERE id = #{id}")
    int updateCourse(Course course);

    // 查询课程
    @Select("SELECT * FROM courses WHERE id = #{id}")
    Course selectCourseById(int id);

    // 查询所有课程
    @Select("SELECT * FROM courses")
    List<Course> selectAllCourses();

    @Select("SELECT count(*) FROM courses where teacher_id = #{id}")
    int countCoursesByTeacherId(@Param("id") int id);

    // 联合查询课程及其教师和分类信息
    @Select("SELECT " +
            "c.id, " +
            "c.title, " +
            "c.description, " +
            "c.created_at AS createdAt, " +
            "c.status, " +
            "t.real_name AS teacherRealName, " +
            "t.introduction AS teacherIntroduction, " +
            "t.avatar AS teacherAvatar, " +
            "cc.name AS categoryName, " +
            "cc.description AS categoryDescription " +
            "FROM courses c " +
            "JOIN teachers t ON c.teacher_id = t.id " +
            "JOIN course_categories cc ON c.category_id = cc.id" +
            " and c.id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "title", column = "title"),
            @Result(property = "description", column = "description"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "status", column = "status"),
            @Result(property = "teacherRealName", column = "teacherRealName"),
            @Result(property = "teacherIntroduction", column = "teacherIntroduction"),
            @Result(property = "teacherAvatar", column = "teacherAvatar"),
            @Result(property = "categoryName", column = "categoryName"),
            @Result(property = "categoryDescription", column = "categoryDescription")
    })
    CourseInfo getCourseById(@Param("id") int id);

    @Select("SELECT " +
            "c.id, " +
            "c.title, " +
            "c.description, " +
            "c.created_at AS createdAt, " +
            "c.status, " +
            "t.real_name AS teacherRealName, " +
            "t.introduction AS teacherIntroduction, " +
            "t.avatar AS teacherAvatar, " +
            "cc.name AS categoryName, " +
            "cc.description AS categoryDescription " +
            "FROM courses c " +
            "JOIN teachers t ON c.teacher_id = t.id " +
            "JOIN course_categories cc ON c.category_id = cc.id")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "title", column = "title"),
            @Result(property = "description", column = "description"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "status", column = "status"),
            @Result(property = "teacherRealName", column = "teacherRealName"),
            @Result(property = "teacherIntroduction", column = "teacherIntroduction"),
            @Result(property = "teacherAvatar", column = "teacherAvatar"),
            @Result(property = "categoryName", column = "categoryName"),
            @Result(property = "categoryDescription", column = "categoryDescription")
    })
    List<CourseInfo> getAllCourses();


}
